package com.dimanche.library_base.utils;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.Base64;

import com.dimanche.library_base.app.BaseApplication;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

/**
 * author : Dimanche
 * date   : 2020/8/179:22
 * desc   :
 * version: 1.0
 */
public class OtherUtils {
    private final static String TAG = "OtherUtils";

    /**
     * base64字符串转bitmap
     *
     * @param base64Str
     * @return
     */
    public static Bitmap base64ToBitmap(String base64Str) {
        if (base64Str == null) {
            return null;
        }
        Bitmap bitmap = null;
        try {
            byte[] bytes = Base64.decode(base64Str, Base64.DEFAULT);
            bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
        } catch (Exception e) {
            bitmap = null;
        }

        return bitmap;
    }


    /**
     * 两个String类型的数字相减
     *
     * @param str1
     * @param str2
     * @return
     */
    public static int subtract(String str1, String str2) {
        if (TextUtils.isEmpty(str1)) {
            str1 = "0";
        }
        if (TextUtils.isEmpty(str2)) {
            str2 = "0";
        }
        int count = 0;
        try {
            count = Integer.parseInt(str1) - Integer.parseInt(str2);
        } catch (Exception e) {
            LogUtils.loge(TAG, "subtract 数据格式不正确", e);
        }
        return count;
    }

    /**
     * 获取版本号
     *
     * @return
     */
    public static String getVersionCodeName() {
        String versionStr = "";
        PackageManager manager = AppUtils.getApplication().getPackageManager();
        try {
            PackageInfo info = manager.getPackageInfo(AppUtils.getApplication().getPackageName(), 0);
            versionStr = info.versionName + "";
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return versionStr;
    }

    /**
     * 获取本机IP
     *
     * @return
     */
    public static String getLocalIpAddress() {
        String ipAddress = "";
        try {
            for (Enumeration<NetworkInterface> en = NetworkInterface
                    .getNetworkInterfaces(); en.hasMoreElements(); ) {
                NetworkInterface intf = en.nextElement();
                for (Enumeration<InetAddress> enumIpAddr = intf
                        .getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
                    InetAddress inetAddress = enumIpAddr.nextElement();
                    if (!inetAddress.isLoopbackAddress()) {
                        ipAddress = inetAddress.getHostAddress();
                        if (!ipAddress.contains("::")) {
                            LogUtils.logi(TAG, inetAddress);
                            return inetAddress.getHostAddress();
                        }

                    } else
                        continue;
                }
            }
        } catch (SocketException ex) {
        }
        return ipAddress;
    }

    /**
     * 让byte数组中的字节两两对调，比如[97,0,98,0]对调完后[0,97,0,98]
     *
     * @param bytes
     * @return
     */
    public static byte[] exchangeByte(byte[] bytes) {
        if (bytes == null || bytes.length < 2) {
            return null;
        }
        byte[] copyByte = new byte[bytes.length];
        for (int i = 0; i < bytes.length; i++) {
            if (i % 2 != 0) {//单数跳过
                continue;
            }
            if (i >= bytes.length - 1) {//是否是最后一个
                break;
            }
            byte byte1 = bytes[i];
            byte byte2 = bytes[i + 1];
            copyByte[i] = byte2;
            copyByte[i + 1] = byte1;
        }
        return copyByte;
    }


}
